package src.Packets;

import src.Util.Stream;
import src.Players.*;
import src.Main;

public class Equipment
{
    private String[] capes = {"cape","Cape"};
    private String[] hats = {"helm","hood","coif","Coif","hat","partyhat"};
    private String[] boots = {"boots","Boots"};
    private String[] gloves = {"gloves","gauntlets","Gloves","vambraces","vamb"};
    private String[] shields = {"kiteshield","sq shield","Toktz-ket"};
    private String[] amulets = {"amulet","necklace","Amulet of"};
    private String[] arrows = {"arrow","arrows","arrow(p)","arrow(+)","arrow(s)","bolt","Bolt rack"};
    private String[] rings = {"ring"};
    private String[] body = {"platebody","chainbody","robetop","leathertop"};
    private String[] legs = {"platelegs","plateskirt","skirt"};
    private String[] weapons = {"scimitar","longsword","sword","longbow","shortbow","dagger","mace","halberd","spear",
    "Abyssal whip","axe","flail","crossbow","Torags hammers","dagger(p)","dagger(+)","dagger(s)","spear(p)","spear(+)",
    "spear(s)","spear(kp)","maul","dart","dart(p)","javelin","javelin(p)","knife","knife(p)","Longbow","Shortbow",
    "Crossbow","Toktz-xil","Toktz-mej","Tzhaar-ket"};
    /* Fullbody is an item that covers your arms. */
    private String[] fullbody = {"platebody","Ahrims robetop","Karils leathertop"};
    /* Fullhat covers your head but not your beard. */
    private String[] fullhat = {"med helm","coif","Dharoks helm","hood"};
    /* Fullmask covers your entire head. */
    private String[] fullmask = {"full helm","mask","Veracs helm","Guthans helm","Torags helm","Karils coif"};

    public Equipment()
    {
    }

    public Equipment(Player p)
    {
        if(p == null || p.inStream == null)
        {
            return;
        }
        int wearId = p.inStream.readUnsignedWord();
        int index = p.inStream.readUnsignedWordA();
        int interfaceId = p.inStream.readUnsignedWordA(); //Just a guess.
        if(p.items[index] == (wearId + 1))
        {
            int targetSlot = itemType(wearId);
            if(targetSlot == -1)
            {
                Main.m.log("[" + p.playerIndex + "-" + p.username + "] Unsupported item " + wearId);
                return;
            }
            p.attackingNPC = false;
            p.attackingPlayer = false;
            p.attackNPC = 0;
            p.attackPlayer = 0;
            int wearAmt = p.itemsN[index];
            if(wearId >= 0 && wearAmt > 0)
            {
                PlayerItems pi = new PlayerItems();
                pi.deleteItem(p, wearId, index, wearAmt);
                if(p.equipment[targetSlot] >= 0 && (wearId != p.equipment[targetSlot] || !Main.m.ge.item.stackable(wearId)))
                {
                    pi.addItem(p, p.equipment[targetSlot], p.equipmentN[targetSlot]);
                    p.equipmentN[targetSlot] = 0;
                }
                p.equipment[targetSlot] = wearId;
                p.equipmentN[targetSlot] += wearAmt;
                p.frame.setEquipment(p, wearId, p.equipmentN[targetSlot], targetSlot);
                p.appearanceUpdateRequired = true;
                p.updateRequired = true;
                pi = null;
            }
        }
        else
        {
            //Player is trying to cheat.
        }
    }

    public int itemType(int item)
    {
        String weapon = Main.m.ge.item.getItemName(item);
        for(int i = 0; i < capes.length; i++)
        {
            if(weapon.endsWith(capes[i]) || weapon.startsWith(capes[i]))
                return 1;
        }
        for(int i = 0; i < hats.length; i++)
        {
            if(weapon.endsWith(hats[i]) || weapon.startsWith(hats[i]))
                return 0;
        }
        for(int i = 0; i < boots.length; i++)
        {
            if(weapon.endsWith(boots[i]) || weapon.startsWith(boots[i]))
                return 10;
        }
        for(int i = 0; i < gloves.length; i++)
        {
            if(weapon.endsWith(gloves[i]) || weapon.startsWith(gloves[i]))
                return 9;
        }
        for(int i = 0; i < shields.length; i++)
        {
            if(weapon.endsWith(shields[i]) || weapon.startsWith(shields[i]))
                return 5;
        }
        for(int i = 0; i < amulets.length; i++)
        {
            if(weapon.endsWith(amulets[i]) || weapon.startsWith(amulets[i]))
                return 2;
        }
        for(int i = 0; i < arrows.length; i++)
        {
            if(weapon.endsWith(arrows[i]) || weapon.startsWith(arrows[i]))
                return 13;
        }
        for(int i = 0; i < rings.length; i++)
        {
            if(weapon.endsWith(rings[i]) || weapon.startsWith(rings[i]))
                return 12;
        }
        for(int i = 0; i < body.length; i++)
        {
            if(weapon.endsWith(body[i]) || weapon.startsWith(body[i]))
                return 4;
        }
        for(int i = 0; i < legs.length; i++)
        {
            if(weapon.endsWith(legs[i]) || weapon.startsWith(legs[i]))
                return 7;
        }
        for(int i = 0; i < weapons.length; i++)
        {
            if(weapon.endsWith(weapons[i]) || weapon.startsWith(weapons[i]))
                return 3;
        }
        return -1;
    }

    public boolean isFullbody(int itemId)
    {
        String weapon = Main.m.ge.item.getItemName(itemId);
        for(int i = 0; i < fullbody.length; i++)
        {
            if(weapon.endsWith(fullbody[i]))
            {
                return true;
            }
        }
        return false;
    }

    public boolean isFullhat(int itemId)
    {
        String weapon = Main.m.ge.item.getItemName(itemId);
        for(int i = 0; i < fullhat.length; i++)
        {
            if(weapon.endsWith(fullhat[i]))
            {
                return true;
            }
        }
        return false;
    }

    public boolean isFullmask(int itemId)
    {
        String weapon = Main.m.ge.item.getItemName(itemId);
        for(int i = 0; i < fullmask.length; i++)
        {
            if(weapon.endsWith(fullmask[i]))
            {
                return true;
            }
        }
        return false;
    }
}